package com.open4cn.simulator.answer.xrite.x7800;

import com.open4cn.simulator.core.answer.AbstractAnswer;
import com.open4cn.simulator.core.answer.AnswerModeEnum;
import com.open4cn.simulator.core.answer.AnswerResponse;
import com.open4cn.simulator.core.ext.XriteStatus;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description
 * @Email yinxiaopeng@zj.tech
 * @date 2022-07-21 18:11
 */
public class GetMeasAnswer extends AbstractAnswer {

    public GetMeasAnswer() {
        super("xrite7800/getMeas", AnswerModeEnum.SUCCESS);
    }

    public AnswerResponse answer(XriteStatus xriteStatus, String instruction) {
        if (!AnswerModeEnum.FAIL.equals(getAnswerMode())) {
            getActions().forEach(action -> {
                action.action(xriteStatus, instruction);
            });
        }
        AnswerResponse response = getAnswer(xriteStatus);
        return response;
    }

    /**
     * 获取应答结果
     *
     * @param xriteStatus
     * @return
     */
    private AnswerResponse getAnswer(XriteStatus xriteStatus) {
        switch (getAnswerMode()) {
            case SUCCESS:
                return AnswerResponse.success(success(xriteStatus));
            case FAIL:
                return AnswerResponse.fail(fail(xriteStatus));
            case LOOP:
                return AnswerResponse.success(getLoop());
            case INPUT:
                Object input = pollInput();
                if (null != input) {
                    return AnswerResponse.success(input);
                }
            default:
                return AnswerResponse.success(success(xriteStatus));
        }
    }


    /**
     * 成功应答
     * MEAS 0x00 0.000 0.000 0.000 0.000 43.416 42.303 41.190 39.891 38.415 36.349 35.207 34.073 31.989 30.449 29.638 28.143 26.786 27.563 29.160 29.214 29.529 34.062 43.671 55.174 64.211 69.211 70.409 69.644 69.893 73.073 79.900 87.273 91.945 94.147 95.150 95.571 95.735 95.607 95.595 95.485
     *
     * @param xriteStatus
     * @return
     */
    private Object success(XriteStatus xriteStatus) {
        StringBuilder builder = new StringBuilder();
        builder.append("MEAS 0x00 0.000 0.000 0.000 0.000 43.416 42.303 41.190 39.891 38.415 36.349 35.207 34.073 31.989 30.449 29.638 28.143 26.786 27.563 29.160 29.214 29.529 34.062 43.671 55.174 64.211 69.211 70.409 69.644 69.893 73.073 79.900 87.273 91.945 94.147 95.150 95.571 95.735 95.607 95.595 95.485");
        return builder.toString();
    }


    /**
     * 失败应答
     * MEAS 0x74 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
     *
     * @param xriteStatus
     * @return
     */
    private Object fail(XriteStatus xriteStatus) {
        StringBuilder builder = new StringBuilder();
        builder.append("MEAS 0x74 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000");
        return builder.toString();
    }
}
